/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Puyo;
import java.io.*;

/**
 *
 * @author Ming Li
 */
public class PuyoConfig {
    private byte  version;
    private String currentPlayerName;
    private short soundVolume;
    
    public PuyoConfig(){
        currentPlayerName = "SuperGamer";
        soundVolume = 40;
        version = 1;
    }
            
    public void write()
    {
        PuyoIO.write("Config",configToByteArray());
    }
    
    public void read()
    {
        byte[] data = PuyoIO.read("Config");
        if(data != null)            
            byteArrayToConfig(data);
    }
    
    private byte[] configToByteArray(){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream os = new DataOutputStream(baos);
        // stream
        try {
            os.writeByte(1);
            os.writeUTF(getCurrentPlayerName());
            os.writeShort(getSoundVolume());            
            os.close();
        } catch( IOException ex ) {
            System.out.println( ex.toString() );
        }
        return baos.toByteArray();
    }
    
    private void byteArrayToConfig(byte[] data){
        DataInputStream is = new DataInputStream( new ByteArrayInputStream(data) );
        try {
            version = is.readByte();
            if(version == 1){
              setCurrentPlayerName(is.readUTF());
              setSoundVolume(is.readShort());
            }
            is.close();
        } catch( IOException ex ) {
            System.out.println(ex.toString() );
        }
    }

    public String getCurrentPlayerName() {
        return currentPlayerName;
    }

    public void setCurrentPlayerName(String currentPlayerName) {
        this.currentPlayerName = currentPlayerName;
    }

    public short getSoundVolume() {
        return soundVolume;
    }

    public void setSoundVolume(short soundVolume) {
        this.soundVolume = soundVolume;
    }
}
